<?php
	if(!defined('__HIGHSCORELIST_PHP__'))
	{
		define('__HIGHSCORELIST_PHP__', true);
		
		include("Highscore.php");
		include("IXMLable.php");
		include("ArrayList.php");
		include("HighscoreArrayList.php");
		
		/**
		 * A list of highscores, dealing with loading, saving and adding highscores,
		 * sorting them, and finding the top 10 highscores. Implements the IXMLable interface
		 */
		class HighscoreList implements IXMLable
		{
			/**
			 * @var The ArrayList containing all the highscores
			 */
			private $m_ArrayList;
			/**
			 * @var The database
			 */
			private $m_DataBase;
			
			/**
			 * @method Default constructor, initializes members
			 */
			function __construct()
			{
				$this->m_ArrayList = new HighscoreArrayList();
			}
			
			/**
			 * @method Adds a highscore to the list
			 * 
			 * @param $highscore The Highscore to add
			 */
			function addHighscore(Highscore $highscore)
			{
				$name = $highscore->getName();
				$level = $highscore->getLevel();
				$score = $highscore->getScore();
				$difficulty = $highscore->getDifficulty();
				$time = $highscore->getDate() . " " . $highscore->getTime();
				
				$this->connectToDataBase();
				
				$query = "INSERT INTO 3tris (name, level, score, difficulty, time) VALUES ('" . $name . "', '" . $level . "', '" . $score . "', '" . $difficulty . "', '" . $time . "')";
				$this->m_Result = mysql_query($query) or die("failed to insert: " . mysql_error());
				
				$this->disconnectFromDataBase();
			}
			
			/**
			 * @method Gets the top 10 elements of this highscore list in descending order. If there is less than
			 * 10 elements in this highscore list, all elements are returned, in descending order.
			 * 
			 * @return The top 10 elements of this highscore list as a atring in XML format
			 */
			function top10($difficulty)
			{
				$this->connectToDataBase();
				
				$query = "SELECT name, level, score, time FROM 3tris WHERE difficulty = '" . $difficulty . "' ORDER BY score DESC, level ASC, time ASC LIMIT 10";
				$result = mysql_query($query) or die("query failed: " . mysql_error());
				
				$xmlString = "<?xml version='1.0' encoding='utf-8'?>\n";
				$xmlString .= "<html>\n";
				$xmlString .= "<head><title>XML top 10</title></head>\n";
				$xmlString .= "<body>\n";
				$xmlString .= "<highscores>\n\n";
				
				while($line = mysql_fetch_array($result, MYSQL_ASSOC))
				{
					$array = new ArrayList();
					
					foreach($line as $col_value)
					{
						$array->add($col_value);
					}
					
					if($array->size() == 4)
					{
						$highscore = new Highscore($array->get(0), $array->get(1), $array->get(2), substr($array->get(3), 2, 8), $difficulty, substr($array->get(3), 11, 8));
						$xmlString .= $highscore->toXML();
					}
				}
				
				$xmlString .= "</highscores>\n";
				$xmlString .= "</body>\n";
				$xmlString .= "</html>\n\n";
				
				mysql_free_result($result);
				$this->disconnectFromDataBase();
				
				return $xmlString;
			}
			
			/**
			 * @method Gets the highest score found in the database
			 * 
			 * @return The highest score found in the database
			 */
			function highscore($difficulty)
			{
				$this->connectToDataBase();
				
				$query = "SELECT score FROM 3tris WHERE difficulty = '" . $difficulty . "' ORDER BY score DESC LIMIT 1";
				$result = mysql_query($query) or die("query failed: " . mysql_error());
				
				$highestScore = 0;
				
				if($line = mysql_fetch_array($result, MYSQL_ASSOC))
				{
					foreach($line as $colValue)
					{
						$highestScore = $colValue;
					}
				}
				
				return $highestScore;
			}
			
			/**
			 * @implementation Converts this class to an XML representation
			 * 
			 * @return A string containing this classes XML representation
			 */
			function toXML()
			{
				$xmlString = "<?xml version='1.0' encoding='utf-8'?>\n";
				$xmlString .= "<highscores>\n";
				
				$size = $this->m_ArrayList->size();
				$index = 0;
				$highscore = NULL;
				
				for($index = 0; $index < $size; $index++)
				{
					$highscore = $this->m_ArrayList->get($index);
					
					if($highscore != NULL)
					{
						$xmlString .= $highscore->toXML();
						$xmlString .= "<br/>\n";
					}
				}
				
				$xmlString .= "</highscores>";
				
				return $xmlString;
			}
			
			/**
			 * @override Gets a string representation of this highscore list formatted as xml
			 * 
			 * @return A string representation of this highscore list
			 */
			function __toString()
			{
				$this->m_ArrayList->sortArrayList();
				$this->m_ArrayList->reverseArrayList();
				$str = $this->toXML();
				
				return $str;
			}
			
			private function connectToDataBase()
			{
				$this->m_DataBase = mysql_connect("THE ROOT OF THE SERVER HOSTING MYSQL", "YOUR USERNAME ON THE MYSQL SERVER HERE", "INSERT PASSWORD HERE") or die("could not connect to databse: " . mysql_error());
				mysql_select_db("david") or die("could not select database");
			}
			
			private function disconnectFromDataBase()
			{
				mysql_close($this->m_DataBase);
			}
			
			private function addHighscoreInternally(Highscore $highscore)
			{
				$this->m_ArrayList->add(highscore);
			}
		}
	}
?>
